package com.tang.thepath;


import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;

import com.tang.thepath.db.DefaultDBHelper;
import com.tang.thepath.model.RequestModel;
import com.tang.thepath.service.BizService;
import com.tang.thepath.service.HttpSubscriber;

import java.util.HashMap;
import java.util.Map;

public class PositionIntentService extends IntentService {

    private DefaultDBHelper dbHelper;


    public PositionIntentService() {
        super("PositionIntentService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        dbHelper = DefaultDBHelper.getInstance(getApplicationContext());
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        RequestModel.ReportPositionRequest request = new RequestModel.ReportPositionRequest();
        request.outsignId = intent.getIntExtra("outsign_id",0);
        request.latitude = intent.getDoubleExtra("lat",0);
        request.longitude = intent.getDoubleExtra("lng",0);
        request.recordTime = intent.getLongExtra("record_time",0);

        final long insertId = intent.getLongExtra("insert_id",0);
        BizService.reportPosition(request).subscribe(new HttpSubscriber<RequestModel.ReportPositionResponse>() {
            @Override
            protected void onSuccess(RequestModel.ReportPositionResponse reportPositionResponse) {
                Map where = new HashMap();
                where.put("id",String.valueOf(insertId));
                dbHelper.delete("locations",where);//清除数据库
                //Toast.makeText(PositionIntentService.this, "上报结果1:"+reportPositionResponse.id, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
